package com.tools.bitbucket.utils;

import org.apache.commons.lang3.StringUtils;

import java.io.*;

public class FileUtils {
    public static String readFileByLines(File file) {
        BufferedReader reader = null;
        InputStreamReader inputFileReader = null;
        String content = "";
        String tempString = null;
        try {
            inputFileReader = new InputStreamReader(new FileInputStream(file), "utf-8");
            reader = new BufferedReader(inputFileReader);
            // 一次读入一行，直到读入null为文件结束
            while ((tempString = reader.readLine()) != null) {
                content += tempString;
            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e1) {
                }
            }
        }
        return content;
    }

    public static void writerJson(File file, String jsonString) {
        FileOutputStream fos = null;
        try {
            if(!file.exists()){
                file.createNewFile();
            }
            fos = new FileOutputStream(file);
            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fos, "UTF-8");
            outputStreamWriter.write(jsonString);
            outputStreamWriter.close();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 创建仓库文件夹
     *
     * @param baseDirPath    备份地址
     * @param repositoryName 仓库名称
     * @return 仓库文件夹全路径
     */
    public static String crateDir(String baseDirPath, String repositoryName) {
        String repositoryPath = baseDirPath + File.separator + repositoryName;
        if (directoryExists(repositoryPath)) {
            return repositoryPath;
        } else {
            if (new File(repositoryPath).mkdirs()) {
                return repositoryPath;
            } else {
                return null;
            }
        }
    }

    /**
     * 通过git 克隆或拉取文件
     *
     * @param repositoryPath 仓库地址
     * @param projectUrl     http://xxx/scm/xxx/xxx.git
     * @return 最新的commit id
     */
    public static String gitCloneOrPull(String repositoryPath, String projectUrl) {
        String projectName = getProjectName(projectUrl);
        String projectPath = repositoryPath + File.separator + projectName;
        //仓库目录已存在，进入仓库目录，进行更新操作 git pull
        if (directoryExists(projectPath)) {
            BashUtils.execCommand("git pull", projectPath);
        } else {//仓库目录不存在，进入项目目录，进行克隆操作 git clone
            BashUtils.execCommand("git clone " + projectUrl, repositoryPath);
        }
        // 进入仓库目录，进行获取最新commitId的操作
        String latestCommitId = BashUtils.execCommand("git rev-parse HEAD", projectPath);
        return latestCommitId;
    }

    /**
     * 通过克隆地址，获取仓库名称
     * @param projectUrl http克隆地址
     * @return  仓库名称
     */
    public static String getProjectName(String projectUrl) {
        String[] split = projectUrl.split("/");
        String projectName = split[split.length - 1].split(".git")[0];//xxx.git
        return projectName;
    }

    /**
     * 判断文件夹是否存在
     */
    public static Boolean directoryExists(String filePath) {
        File file = new File(filePath);
        return file.exists() && file.isDirectory();
    }

    /**
     * 创建仓库介绍文件
     *
     * @param repositoryPath  仓库文件夹
     * @param repositoryIntro 仓库说明
     */
    public static void updateIntroFile(String repositoryPath, String repositoryIntro) {
        //创建 介绍文件
        File introFile = new File(repositoryPath + File.separator + "intro.txt");
        try {
            if (!introFile.exists()) {
                introFile.createNewFile();
            }
            FileWriter fileWriter = new FileWriter(introFile);
            fileWriter.write(StringUtils.isNotEmpty(repositoryIntro) ? repositoryIntro : "");
            fileWriter.flush();
            fileWriter.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String bdht_integration = crateDir("I:/北斗航天/代码备份/test", "练习项目");
        if (null != bdht_integration) {
            FileUtils.updateIntroFile(bdht_integration, "这是练习项目的简介  新的 哦");
            FileUtils.gitCloneOrPull(bdht_integration, "http://gaoyongxin@192.168.1.250:7990/bitbucket/scm/lxm/beacontest.git");
            FileUtils.gitCloneOrPull(bdht_integration, "http://gaoyongxin@192.168.1.250:7990/bitbucket/scm/lxm/beacontest.git");
            FileUtils.gitCloneOrPull(bdht_integration, "http://gaoyongxin@192.168.1.250:7990/bitbucket/scm/lxm/dubbo-demo.git");
        }
    }
}
